package de.oszimtcc.timetablescreens;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.framework.BaseScreen;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetablecore.Block;
import de.oszimtcc.timetablecore.Block.BlockType;


public class BlockDetailScreen extends BaseScreen
{

	public BlockDetailScreen(BaseActivity pBaseActivity)
    {
		super(pBaseActivity);
		Setup(R.layout.blockdetail);
		SetLastScreen(TimetableScreen.class);
		Logger.Debug("Open BlockDetail");
    }

	public BlockDetailScreen(BaseActivity pBaseActivity,Block pToShow)
    {
		super(pBaseActivity);
		Setup(R.layout.blockdetail);
		SetLastScreen(TimetableScreen.class);
		Logger.Debug("Open BlockDetail");
        		
		Block block = pToShow;

		activity.getRootView().setBackgroundResource(block.GetBlockType() == BlockType.Standard ? R.drawable.normal : block.GetBlockType() == BlockType.Changed ? R.drawable.change : R.drawable.cancel);
		TextView blockNumber = (TextView) activity.findViewById(R.id.blockNumber);
		TextView blockStart = (TextView) activity.findViewById(R.id.blockStart);
		TextView blockEnd = (TextView) activity.findViewById(R.id.blockEnd);
		TextView blockSubject = (TextView) activity.findViewById(R.id.blockSubject);
		TextView blockTeacher = (TextView) activity.findViewById(R.id.blockTeacher);
		TextView blockRoom = (TextView) activity.findViewById(R.id.blockRoom);
		ListView calendarList = (ListView)activity.findViewById(R.id.calendarList);
		
		
		blockNumber.setText(""+block.GetBlockNumber());
		blockStart.setText(block.GetStartTime());
		blockEnd.setText(block.GetEndTime());
		blockSubject.setText(block.GetSubject());
		blockTeacher.setText(block.GetTeacher());
		blockRoom.setText(block.GetRoom());
		
		setTextColorRecursive(activity.getRootView());
		
		blockNumber.setTextColor(Color.BLACK);
    }
	
	protected void setTextColorRecursive(View v) 
	{
		if(v instanceof ViewGroup) 
		{
			for(int i=0; i<((ViewGroup) v).getChildCount(); i++) 
			{
				View iv = ((ViewGroup) v).getChildAt(i);
				if(iv instanceof TextView) 
				{
					TextView text = (TextView)iv;
					if(!text.getText().toString().contains("_"))
						break;
					if(text.getText().toString().contains("c_"))
					{
						text.setTextColor(Color.RED);
						text.setText(text.getText().toString().replace("c_", ""));
					}
					else if(text.getText().toString().contains("n_"))
					{
						text.setTextColor(Color.BLACK);
						text.setText(text.getText().toString().replace("n_", ""));
					}
				} 
				else if(iv instanceof ViewGroup) 
				{
					setTextColorRecursive(iv);
				}  
			}
		}
	}
}
